<?php 
class CalcTime
{
//    const DEBUG = true;
    const DEBUG = false;
    
    
    static $timeStart;
    static public function Start()
    {
        if (!CalcTime::DEBUG) return;
        CalcTime::$timeStart = microtime(true);
    }
    static public function ShowTime($IsExit = false)
    {     
        if (!CalcTime::DEBUG) return;
        
        echo "<br/>Thoi gian: ";
        echo microtime(true) - CalcTime::$timeStart;
        echo " giay<br/>";
        
        if ($IsExit) exit();
    }
}
class CommonValues
{
	//Tien
	static public $gCompCategory= array(
	   'key'=> array(
		   'Main Component'   =>0, 
		   'Sub Component'     =>1,
		   'Raw Material'          =>2),
	   'value'=> array(
                   0=>'Main Component',
                   1=>'Sub Component',
                   2=>'Raw Material')
	);
        
        static public $gCompCategorySimple= array(
	   'key'=> array(
		   'MAIN'   =>0, 
		   'SUB'     =>1,
		   'RAW'          =>2),
	   'value'=> array(
                   0=>'MAIN',
                   1=>'SUB',
                   2=>'RAW')
	);
	
	static public $gBomStatus= array(
	   'key'=> array(
		   'Initial'              =>0, 
		   'In progress'     =>1,
                     'Completed'       =>2,
                     'Revised' =>3,
                     'Rejected'=>4,
                     'Cancel'=>5,),
	   'value'=> array(
                       0=>'Initial', 
                       1=>'In progress',
                       2=>'Completed',
                       3=>'Revised',
                       4=>'Rejected',
                       5=>'Cancel',),
              'action'=> array(
                       0=>'publish', 
                       1=>'revise',
                       2=>'COMPLETED',
                       3=>'UNACTION',
                       4=>'UNACTION',
                       5=>'UNACTION'),
	);
	
	static public $gCBCStatus= array(
	   'key'=> array(
		   'In progress'              =>0, 
		   'Completed'     =>1,
		   'Revised'     =>2,
		   ),
	   'value'=> array(
                       0=>'In progress', 
                       1=>'Completed',
                       2=>'Revised',
                       ),
	);
	
	static public $gMRCategory= array(
	   'key'=> array(
		   'Project'              =>1, 
		   'Stock'     => 2,
                         'Factory'       =>3,
                         'Other'    => 4),
	   'value'=> array(
                       1=>'Project', 
                       2=>'Stock',
                       3=>'Factory',
                       4=>'Other'),
               'display'=>array(
                      'Stock'=>2,
                      'Factory'=>3,),	
	   'dpvalue'=> array(                       
                       2=>'Stock',
                       3=>'Factory', ),
	   'permission'=> array(                       
                      'Stock'=>'EDIT_MRSUBSTK',
                      'Factory'=>'EDIT_MRSUBFTR', ),	
	);
	
	static public $gMRStatus= array(
	   'key'=> array(
		   'Initial'              =>0, 
		   'In progress'     =>1, 
		   'Completed'     =>2,
		   'Revised'     =>3,
		   'Rejected'     =>4,
		   'Cancel'     =>5,
		   ),
	   'value'=> array(
                       0=>'Initial', 
                       1=>'In progress',
                       2=>'Completed',
                       3=>'Revised',
                       4=>'Rejected',
                       5=>'Cancel',
                       ),
              'action'=> array(
                       0=>'publish',
                       1=>'Revise',
                       2=>'COMPLETED',
                       3=>'UNACTION',
                       4=>'UNACTION',
                       5=>'UNACTION',
                       ),	
	);
	
	static public $gMRApprovalStatus= array(
	   'key'=> array(
		   'Initial'              =>0, 
		   'Waiting for approval'              =>1, 
		   'Approved'     =>2,
		   'Cancel'     =>3,
                   'Rejected'     =>4,
		   ),
	   'value'=> array(
                       0=>'Initial', 
                       1=>'Waiting for approval', 
                       2=>'Approved',
                       3=>'Cancel',
                       4=>'Rejected',
                  ),
              'action'=> array(
                       0=>'publish', 
                       1=>'COMPLETED',
                       2=>'COMPLETED',
                       3=>'COMPLETED',
                       4=>'COMPLETED',
                  ),
	);
		
	static public $gPOReceivedStatus= array(
	   'key'=> array(
		   'Initial'              =>0, 
		   'In progress'              =>1, 
		   'Partially Received'     =>2,
		   'Fully Received'=>3,
		   'Cancel'=>4,
		   'Revised'=>5,
		   ),
	   'value'=> array(
                       0=>'Initial', 
                       1=>'In progress', 
                       2=>'Partially Received',
                       3=>'Fully Received',
                       4=>'Cancel',
                       5=>'Revised',
                       )	
	);
	
	static public $gPOType= array(
	   'key'=> array(
		   'Local'              =>0, 
		   'Overseas'              =>1,),
	   'value'=> array(
                       0=>'Local', 
                       1=>'Overseas',)	
	);
	
	static public $gPOShip= array(
	   'key'=> array(
		   'By Land'              =>0, 
		   'By Air'              =>1, 
		   'By Sea'     =>2,),
	   'value'=> array(
                       0=>'By Land', 
                       1=>'By Air', 
                       2=>'By Sea',)	
	);


	static public $gTLStatus= array(
	   'key'=> array(
		   'Initial'     =>0,
		   'Published'   =>1,
		   'Revised'     =>2
		   ),
	   'value'=> array(
                       0=>'Initial',
                       1=>'Published',
                       2=>'Revised'
                       ),
              'action'=> array(
                       0=>'publish',
                       1=>'Revise',
                       2=>'UNACTION'
                       ),
	);
	
	static public $gTallyInType= array(
	   'key'=> array(
		   'Supplier'              =>0, 
		   'Project'              =>1,
		   'Production'     =>2,
		   'Service'     =>3,
               ),
	   'value'=> array(
                       0=>'Supplier', 
                       1=>'Project',
                       2=>'Production',
                       3=>'Service',
               )
	);
	
	static public $gTallyOutType= array(
	   'key'=> array(
		   'Project'              =>0,
		   'Production'              =>1,
		   'Factory'     =>2,
		   'Service'     =>3,
                   'Scrap'     =>4,
               ),
	   'value'=> array(
                       0=>'Project',
                       1=>'Production',
                       2=>'Factory',
                       3=>'Service',
                       4=>'Scrap',
               )
	);
	
	static public $gTempOutType= array(
	   'key'=> array(
		   'Service'              =>0, 
		   'Testing'              =>1, 
		   'Return To Supplier'     =>2,),
	   'value'=> array(
                       0=>'Service', 
                       1=>'Testing', 
                       2=>'Return To Supplier',)	
	);
	
	static public $gApprovalType= array(
	   'key'=> array(
		   'BOM'              =>0,
		   'MRSUB'              =>1, 
		   'MRRAW'     =>2,
		   'PO'       =>3,
                   'MR'       =>4),
	);
	
	static public function echoFilterDate($filters) { 
	   $year_start = date('Y', strtotime('-10 years'));
	   $year_end = date('Y', strtotime('+10 years'));
	   echo select_year_tag('filters[year]', isset($filters['year']) ? $filters['year'] : 0, array('year_start' => $year_start, 'year_end' => $year_end, 'include_blank'=>true));
	   echo select_month_tag('filters[month]', isset($filters['month']) ? $filters['month'] : 0, array('include_blank'=>true));
	}

        static public function formatMoney($number, $fractional=false) {
            if ($fractional) {
                $number = sprintf('%.2f', $number);
            }
            while (true) {
                $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
                if ($replaced != $number) {
                    $number = $replaced;
                } else {
                    break;
                }
            }
            return $number;
        } 
	static public function GenerateAutoNumber($mod, $defaultValue, $prefix , $month=null ){
	   if ($defaultValue)
	       return $defaultValue;
	   //create WO default
	   if ($month)
                $default = $prefix .'-'. date('y').'-'.date('m').'-';
              else
                $default = $prefix .'-'. date('y').'-';
                
              //get max Id
              $c = new Criteria();
              $object = null;
              $number = null;
              switch ($mod) {
                case 'BOM':                    
                  $c->addDescendingOrderByColumn(BOMPeer::WONO );
                  $object = BOMPeer::doSelectOne($c);
                  if ($object)
                    $number = $object->getWono();
                  break;
                case 'MR':                    
                  $c->addDescendingOrderByColumn(MRPeer::MRNO );
                  $object = MRPeer::doSelectOne($c);                  
                  if ($object)
                    $number = $object->getMrno();
                  break;
                case 'MR2':                    
                  $c->addDescendingOrderByColumn(MR2Peer::MRNO );
                  $object = MR2Peer::doSelectOne($c);
                  if ($object)
                    $number = $object->getMrno();
                  break;
                case 'MR3':                    
                  $c->addDescendingOrderByColumn(MR3Peer::MRNO );
                  $object = MR3Peer::doSelectOne($c);
                  if ($object)
                    $number = $object->getMrno();
                  break;
                case 'PO':                    
                  $c->addDescendingOrderByColumn(POPeer::PONO );
                  $object = POPeer::doSelectOne($c);
                  if ($object)
                    $number = $object->getPono();                  
                  break;
                case 'TALLYIN':                    
                  $c->addDescendingOrderByColumn(TallyInPeer::TALLYINCODE );
                  $object = TallyInPeer::doSelectOne($c);                  
                  if ($object)
                    $number = $object->getTallyincode();
                  break;
                case 'TALLYOUT':                    
                  $c->addDescendingOrderByColumn(TallyOutPeer::TALLYOUTCODE );
                  $object = TallyOutPeer::doSelectOne($c);
                  if ($object)
                    $number = $object->getTallyoutcode();                  
                  break;
                case 'TpOUT':                    
                  $c->addDescendingOrderByColumn(TemporaryOutputPeer::TEMPNO );
                  $object = TemporaryOutputPeer::doSelectOne($c);
                  if ($object)
                    $number = $object->getTempno();
                  break;
              case 'PS':
                  $c->addDescendingOrderByColumn(PhysicalQtyPeer::PSNO);
                  $object = PhysicalQtyPeer::doSelectOne($c);
                  if ($object)
                    $number = $object->getPsno();
                  break;

              }
              
              if ($number) {
                $begin = substr($number, 0, strrpos($number, '-')+1);
                $number = substr($number, strrpos($number, '-') +1);
                
                if ($begin ==$default)
                    $number = intval($number) + 1;
                else $number ='1';
              }
              else $number='1';

              if ($mod =='PO')
                $default.= sprintf('%04d', $number);
              else
                $default.= sprintf('%03d', $number);
              
              return $default;
	}

        static function ConvertDateToEN($date){
            list($d, $m, $y) = split('[/.-]', $date);
            return "$y-$m-$d";
        }

        static function FormatCurrency($num, $isFloat = true)
        {            
            if ($isFloat)
            {
                $num = sprintf('%.2f', $num);            
                $i = strpos($num, '.');
                $tp = substr($num, $i + 1);
                $num = substr($num, 0, $i);
            } else 
            {
                $tp = '';
                $num = "$num";
            }
            
            $x = '';
            for ($i = 0; $i < strlen($num); $i++)
            {
                if ($i > 0 && $i %3 ==0)
                {
                    $x = $num[strlen($num) - $i -1] . ',' . $x;
                }else $x = $num[strlen($num) - $i - 1] . $x;
            }
            if ($tp != '')
                $x .= '.' . $tp;
            return $x;
        }
        
        static function CurrencyToNumber($num)
        {
            return floatval(str_replace(',', '', $num));
        }
        static function RateCurrency($currency)
        {
            if ($currency == 'USD') return 1;
            if ($currency == 'VND')
            {
                $c = new Criteria();
                $c->add(MyConfigPeer::IDS, 'USDVND');
                $rateConfig = MyConfigPeer::doSelectOne($c);
                if ($rateConfig == null)
                    return 1;
                return floatval($rateConfig->getDescription());
            }
            if ($currency == 'SGD')
            {
                $c = new Criteria();
                $c->add(MyConfigPeer::IDS, 'USDSGD');
                $rateConfig = MyConfigPeer::doSelectOne($c);
                if ($rateConfig == null)
                    return 1;
                return floatval($rateConfig->getDescription());
            }
            return 1;
        }
	
    
}
?>